# Paper: Is it still the economy? Economic voting in polarized politics
# Individual-level simulation
# Replication: Figure 6
remove(list = ls())

library(conflicted)
library(tidyverse)
library(dplyr)
conflict_prefer("filter","dplyr")
library(ggplot2)
library(here) # When we have an R-project, it reads files without setting an directory
conflict_prefer("here", "here")
library(nnet)
library(ggeffects)
library(MASS)
conflict_prefer("select","dplyr")

load(here("Final_Paper", "R&R", "IndLevel.RData"))

table(model_data$partisan)

model_data <- model_data %>% 
  mutate(partisan = ifelse(ptd_id3 == "Ind", "Ind", NA),
         partisan = ifelse(ptd_id3 == "Rep" & 
                             rep_inc == 1, "Inpart", partisan),
         partisan = ifelse(ptd_id3 == "Rep" & 
                             rep_inc == 0, "Outpart", partisan),
         partisan = ifelse(ptd_id3 == "Dem" & 
                             rep_inc == 0, "Inpart", partisan),
         partisan = ifelse(ptd_id3 == "Dem" & 
                             rep_inc == 1, "Outpart", partisan))

summary(ml_inc <- MASS::polr(econ_eval ~ percep_polar*partisan + 
                               female + black + hispanic + asian + other_race + 
                               log_age + college + income + election, 
                             data = model_data, Hess=TRUE))


ml_inc2 <- ml_inc %>% 
  ggeffect(terms = c("partisan", "percep_polar[0.1, 0.5, 1]"), 
           ci.lvl = 0.95)
glimpse(ml_inc2)

ml_inc2 <- data.frame(ml_inc2)

ml_inc2 <- ml_inc2 %>% 
  rename(partisan = x)

ml_inc2 <- ml_inc2 %>% 
  mutate(econ_eva = ifelse(response.level == "X1", "Worse", NA),
         econ_eva = ifelse(response.level == "X2", "Same", econ_eva),
         econ_eva = ifelse(response.level == "X3", "Better", econ_eva))

g_inc <- ml_inc2 %>% 
  mutate(partisan = factor(partisan,
                           levels = c("Inpart", 
                                      "Ind",
                                      "Outpart"),
                           labels = c("President's Party", 
                                      "Independent",
                                      "Opposing Party"))) %>% 
  mutate(econ_eva = factor(econ_eva,
                           levels = c("Worse", 
                                      "Same",
                                      "Better"),
                           labels = c("Worse", 
                                      "Same",
                                      "Better"))) %>% 
  mutate(group = as.factor(group)) %>% 
  #mutate(response.level = as.factor(response.level)) %>%
  filter(econ_eva == "Worse") %>% 
  #mutate(x = as.factor(x)) %>%
  #filter(response.level == "Inc") %>% 
  #filter(group == 0.33 | group == 1) %>% 
  ggplot(aes(shape = group)) + 
  #geom_hline(yintercept = 0, colour = "black", lty = 2, size = .75) +
  geom_pointrange(aes(x = partisan, y = predicted, 
                      ymin = conf.low, ymax = conf.high),
                  lwd = .85, position = position_dodge(width = .5),
                  color = "darkslategray") + 
  theme_light() + 
  labs(x = "Partisan Attachment", 
       y = "Probability of Negative Economic Evaluation") +
  #scale_color_manual(name = "Partisanship:", 
  #               values = c("gray17", "gray37", "gray57"),
  #                labels = c("Inpart", "Ind", "Outpart")) +
  scale_shape_manual(name = "Perceived Ideological Gap:",
                     labels = c("0.1", "0.5", "1"),
                     values = c(15, 16, 17)) +
  #scale_fill_manual(name = "Partisanship:",
  #                 values = c("gray17", "gray37", "gray57"), 
  #                labels = c("Inpart", "Ind", "Outpart")) +
  scale_y_continuous(breaks = c(0.8, 0.6, 0.4, 0.2, 0),
                     limits = c(0, 0.8)) +
  theme(axis.text.x = element_text(hjust = 0.5, size = 12),
        axis.text.y = element_text(size = 10),
        axis.title.x = element_text(size = 12),
        axis.title.y = element_text(size = 12),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 12),
        legend.position = "bottom",
        legend.direction = "horizontal",
        plot.title = element_text(size = 14))
g_inc

